package com.lqz.tools;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import java.util.List;
import java.util.Vector;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import javax.swing.ListSelectionModel;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableColumn;
import javax.swing.table.TableColumnModel;

/**
 * @ClassName: PotMain
 * @Description: 项目进入点
 * @author 罗强争
 * @date 2017年6月21日 上午10:15:59
 *
 */
public class PotMain extends JFrame {

	/**
	 * @Fields serialVersionUID : TODO(用一句话描述这个变量表示什么)
	 */
	private static final long serialVersionUID = 1L;

	private PotTestUrl potTestUrl;
	public static JButton fileSelect, // 弹出选择文件对话框
			fileRead, // 读取文件内容
			startTest, // 开始测试直播源地址是否可用
			export, // 导出
			removeRepetition;// 去除重复记录
	public static JTextArea fileName;
	public static JTable tableList;// 表格

	public static JLabel createFilePathLable;
	public static JTextField createFilePath;

	/**
	 * 初始化窗口
	 */
	public PotMain() {
		// 设置容器为空布局，绝对定位
		this.setLayout(null);

		fileName = new JTextArea();
		fileName.setBounds(20, 20, 200, 70);
		fileName.setLineWrap(true);
		this.add(fileName);

		fileSelect = new JButton("选择文件");
		fileSelect.setBounds(250, 20, 100, 30);
		fileSelect.addActionListener(new fileSelectAction());
		this.add(fileSelect);

		fileRead = new JButton("加载列表");
		fileRead.setBounds(400, 20, 100, 30);
		fileRead.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				readFile();
			}
		});

		removeRepetition = new JButton("去除重复");
		removeRepetition.setBounds(540, 20, 100, 30);
		removeRepetition.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				removeRepetition();
			}
		});

		startTest = new JButton("开始测试");
		startTest.setBounds(250, 60, 100, 30);
		startTest.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				testUrl();
			}
		});

		export = new JButton("导出文件");
		export.setBounds(400, 60, 100, 30);
		export.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				export();
			}
		});
		createFilePathLable = new JLabel();
		createFilePathLable.setBounds(20, 100, 120, 30);
		createFilePathLable.setText("文件产生路径:");

		createFilePath = new JTextField();
		createFilePath.setBounds(120, 100, 500, 30);

		this.add(fileRead);
		this.add(removeRepetition);
		this.add(startTest);
		this.add(export);
		this.add(createFilePathLable);
		this.add(createFilePath);

		String[] columnNames = { "序号", "名称", "地址", "状态" };

		DefaultTableModel tableModel = new DefaultTableModel(null, columnNames);

		tableList = new JTable(tableModel);
		// table = new JTable(new MyTableModel());
		tableList.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);// 设置单选
		// tableList.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);// 关闭表格自动调整

		/* 用JScrollPane装载JTable，这样超出范围的列就可以通过滚动条来查看 */
		JScrollPane scroll = new JScrollPane(tableList);
		scroll.setBounds(20, 150, 660, 300);
		this.add(scroll);

		// 设置标题
		this.setTitle("http直播源检测工具");
		// 设置窗口的关闭策略
		this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		// 设置窗口大小
		this.setSize(750, 550);
		// 设置窗口居中，放在窗口大小后面，null表示桌面
		this.setLocationRelativeTo(null);
		// 将窗口设置为显示,要写在最后一句
		this.setVisible(true);
	}

	public static void main(String[] args) {
		new PotMain();
	}

	private void removeRepetition() {
		PotRemoveRepetition t = new PotRemoveRepetition();
		Thread t1 = new Thread(t);
		t1.start();
	};

	private void export() {
		DefaultTableModel tableModel = (DefaultTableModel) tableList.getModel();
		Vector<?> rowData = tableModel.getDataVector();
		ArrayList<PotModel> exportList = new ArrayList<PotModel>();
		int j = 0;
		for (int i = 0, n = rowData.size(); i < n; i++) {

			String rowString = new String(rowData.get(i).toString());
			String[] row = rowString.substring(1, rowString.length() - 1).split(",");

			if (row[3].trim().equals("可用")) {
				j++;
				PotModel potModel = new PotModel();
				potModel.setNum(j);
				potModel.setTitle(row[1].trim());
				potModel.setFile(row[2].trim());
				exportList.add(potModel);
			}
		}
		createFilePath.setText(Potplay.writeDPL(exportList));
	};

	private void testUrl() {
		//第二种使用ReentrantLock
		potTestUrl=new PotTestUrl();
		 int count = tableList.getModel().getRowCount();
		for(int i=0;i<20;i++){
			Thread t = new Thread(potTestUrl);
			t.start();
		}
		//第一种使用 synchronized
//		if (potTestUrl.flag) {
//			potTestUrl.flag = false;
//		} else {
//			potTestUrl.flag = true;
//		}
//
//		System.out.println(potTestUrl.flag);
//		Thread t1 = new Thread(potTestUrl);
//		t1.start();
//		Thread t2 = new Thread(potTestUrl);
//		t2.start();

	};

	private void readFile() {
		// TODO Auto-generated method stub
		DefaultTableModel tableModel = (DefaultTableModel) tableList.getModel();
		tableModel.setRowCount(0);// 清除原有行
		List<PotModel> list = Potplay.readDPL(fileName.getText());
		if (list.size() > 0) {
			for (int i = 0; i < list.size(); i++) {
				PotModel potModel = (PotModel) list.get(i);
				@SuppressWarnings("rawtypes")
				Vector<Comparable> a = new Vector<Comparable>();
				a.addElement(potModel.getNum());
				a.addElement(potModel.getTitle());
				a.addElement(potModel.getFile());
				a.addElement("未开始");
				tableModel.addRow(a);
			}
		}
		/*
		 * hideTableColumn(tableList, 3);
		 * 
		 * hideTableColumn(tableList, 2);
		 * 
		 * hideTableColumn(tableList, 1);
		 */
		// 更新表格
		tableList.updateUI();
		tableList.invalidate();
	}

	// 隐藏表格列
	public void hideTableColumn(JTable table, int column) {
		TableColumnModel tcm = table.getColumnModel();
		// 其实没有移除，仅仅隐藏而已
		TableColumn tc = tcm.getColumn(column);
		tcm.removeColumn(tc);
	}

	public PotTestUrl getPotTestUrl() {
		return potTestUrl;
	}

	public void setPotTestUrl(PotTestUrl potTestUrl) {
		this.potTestUrl = potTestUrl;
	}
}
